package com.njd.test.entity;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.TableMapping;
import com.jfinal.plugin.activerecord.Db;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * 自动生成的Model
 * 
 * @author Johnson Zhang
 *
 */
@SuppressWarnings("serial")
public class Module extends Model<Module> {
	// --------------setter和getter---------------

	public Module setId(Integer id) {
		set("id", id);
		return this;
	}

	public Integer getId() {
		return get("id");
	}

	public Module setStaus(Integer staus) {
		set("staus", staus);
		return this;
	}

	public Integer getStaus() {
		return get("staus");
	}

	public Module setProjectId(Integer projectId) {
		set("project_id", projectId);
		return this;
	}

	public Integer getProjectId() {
		return get("project_id");
	}

	public Module setModuleName(String moduleName) {
		set("module_name", moduleName);
		return this;
	}

	public String getModuleName() {
		return get("module_name");
	}

	// -------------------------------------------
	/***
	 * 数据库操作实体
	 */
	public static final Module dao = new Module();
	/**
	 * 表名
	 */
	public static final String tableName = "module";
	/**
	 * 表主键
	 */
	public static final String primaryKey[] = TableMapping.me()
			.getTable(dao.getClass()).getPrimaryKey();

	/***
	 * 空构造
	 */
	public Module() {
	}

	/***
	 * 有参构造
	 */
	public Module(Integer id, Integer staus, Integer projectId,
			String moduleName) {
		setData(id, staus, projectId, moduleName);
	}

	/***
	 * 数据批量设置
	 */
	public Module setData(Integer id, Integer staus, Integer projectId,
			String moduleName) {
		set("id", id);
		set("staus", staus);
		set("project_id", projectId);
		set("module_name", moduleName);
		return this;
	}

	/***
	 * 按ID逆序查询全部数据
	 * 
	 * @param pageNumber
	 *            页码
	 * @param pageSize
	 *            每页记录数
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> paginate(int pageNumber, int pageSize,
			String para, Object value) throws Exception {
		Object[] o = new Object[] {};
		StringBuffer sb = new StringBuffer();
		sb.append("from " + tableName);
		if (value != null && value != null && value != "") {
			o = new Object[] { value };
			sb.append(" where " + para + "=?");
		}
		sb.append(" order by " + primaryKey[0] + " DESC");
		Long num = Db.queryLong("select count(1) " + sb.toString(), o);
		List<?> data = paginate(pageNumber, pageSize, "select *",
				sb.toString(), o).getList();
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("data", data);
		m.put("num", num);
		return m;
	}
	
	
	public List<Module> getModuleList() {
		return find("SELECT * FROM " + tableName + " WHERE staus = 0");
	}
	
	public List<Module> getModuleListByProjectId(int projectId) {
		return find("SELECT * FROM " + tableName + " WHERE " + R.module.projectId + " = ? AND staus = 0", projectId);
	}

}